package com.xyz.tasks.tdo;

import com.free.exception.SystemException;
import com.free.ext.httpclient.HttpClientUtil;
import com.free.ext.httpclient.HttpConfig;
import com.free.ext.logger.FreeLogger;
import com.free.ext.logger.FreeLoggerFactory;
import com.free.ext.quartz.TaskContainer;
import com.free.ext.quartz.tasker.RemoteUrlTasker;
import com.free.utils.DateUtils;
import com.xyz.domain.constants.ConstantsLotteryTaskFormater;
import com.xyz.domain.table.SLottery;

public class TaskerDealUtils {

    private static final FreeLogger LOG = FreeLoggerFactory.getLogger(TaskerDealUtils.class);

    public static Integer getFixedSpanSeconds(SLottery lotteryInfo){
        int seconds = lotteryInfo.getStepSecs() == null ? 0 : lotteryInfo.getStepSecs();
        if(lotteryInfo.getStepMins() != null && lotteryInfo.getStepMins() > 0){
            seconds = lotteryInfo.getStepMins() * 60;
        }
        return seconds;
    }

    public static void executAndAddTaskAfterSeconds(Integer sleepSeconds, Integer fixedSeconds , String remoteUrl, RemoteUrlTasker tasker){
        try{
            Thread.sleep(sleepSeconds * 1000);
        }catch (Exception ex){
            LOG.info("睡眠后，添加定时任务时发生异常");
        }
        TaskContainer.instance().add(tasker, DateUtils.getCurrentDate(), fixedSeconds);
    }

    public static void executeAndResumeTaskAfterSeconds(Integer seconds, String taskGroup, String taskName){
        try {
            Thread.sleep(seconds * 1000);
        } catch (InterruptedException e) {
            LOG.info("开始任务唤醒，开启定时任务时发生异常", seconds, taskGroup, taskName);
        }
        LOG.info("睡眠了{}秒后，开始恢复定时任务{}-{}", seconds, taskGroup, taskName);
        TaskContainer.instance().resume(taskGroup, taskName);
        new Thread(()->{
            LOG.info("定时任务{}-{}，即将被执行", taskGroup, taskName);
            TaskContainer.instance().runImmediately(taskGroup,taskName);
        }).start();
    }

}
